﻿@model IEnumerable<CoreLims.Models.CoreLims.SampleMethod>

@{
    ViewBag.Title = "方法管理";
}

<div class="row col-md-12 ml-0 vh-3">
    <strong class="font-rfs">方法管理</strong>
</div>
<div class="row col-md-12 ml-0 min-vh-90">
    <div class="row col-md-12 min-vh-90" style="padding-right:0">
        <div class="row col-md-12 vh-5">
            <select id="Sample-Method-Select-Type" class="form-control font-rfs custom-headtoolbar-select">
                @foreach (var item in Model)
                {
                    <option>@item.Type</option>
                }
            </select>
            <button id="Sample-Method-addMethod" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
            <button id="Sample-Method-editMethod" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
            <button id="Sample-Method-delMethod" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
@*             <button id="Sample-Method-testSheet" class="btn btn-rfs"><i class="bi-caret-right-fill" style="color:chartreuse"></i> 测试</button>
 *@        </div>
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 min-vh-85 pre-scrollable">
            <table id="Sample-Method-Table" class="table table-striped table-bordered table-sm table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>代码</th>
                        <th>名称</th>
                        <th>类型</th>
                        <th>描述</th>
                        <th>生效日期</th>
                        <th>有效期至</th>
                        <th>记录单</th>
                        <th>版本号</th>
                        <th>SOP文档</th>
                        <th>状态</th>
                        <th>启用</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal" id="Sample-Method-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="modalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div id="Sample-Method-Modal-Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="Sample-Method-DelMethod" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前选中记录吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="Sample-Method-delMethod-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="Sample-Method-delMethod-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var Sample_Method_Id = "";

        $(document).ready(function () {
            $("#Sample-Method-Select-Type").prepend(new Option("全部"));
            $("#Sample-Method-Select-Type").prepend(new Option("", ""));
            $("#Sample-Method-Select-Type").val("");
        });

        $("#Sample-Method-Select-Type").change(function () {
            var columns = [{ field: "id", visible: false }, { field: "code" }, { field: "name" }, { field: "type" }, { field: "description" },
            { field: "effectiveDate", formatter: setEffectiveDate }, { field: "expireDate", formatter: setExpireDate },
            { field: "recordSheet" }, { field: "version", width: "5%", }, { field: "SopFiles", width: "5%", formatter: setRecordSheet },
            { field: "status", width: "5%", }, { field: "enable", width: "5%", align: "center", formatter: SetMethodEnableCortrol }];

            Sample_Method_Id = "";  /*清空该值*/
            $('#Sample-Method-Table').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#Sample-Method-Table').bootstrapTable({
                method: 'post',
                url: "GetMethodListByType",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit,
                        offset: params.offset,

                        Type: $("#Sample-Method-Select-Type").find("option:selected").text(),  // 传递的参数，这的名称要和控制器里对应的参数名称一样
                    };
                    return data;
                }, //参数
                queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 15,   // 默认页大小
                pageList: [5, 10, 15, 20],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
                onClickRow: function (row, $element, field) {
                    $("#Sample-Method-Table").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    Sample_Method_Id = row.id;
                },
                silent: true,  //刷新事件必须设置
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#Sample-Method-Table').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        });

        function setEffectiveDate(value, row, index) {
            if (!row.effectiveDate) {
                return '';
            } else {
                return row.effectiveDate.split('T')[0];
            }
        }

        function setExpireDate(value, row, index) {
            if (!row.expireDate) {
                return '';
            } else {
                return row.expireDate.split('T')[0];
            }
        }

        function SetMethodEnableCortrol(value, row, index) {
            var strHtml = "";
            if (row.enable) {
                strHtml = "<input type='checkbox' checked onclick='MethodEnableOnClick(this)' data-rowid='" + row.id + "'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='MethodEnableOnClick(this)' data-rowid='" + row.id + "'/>";
            }

            return strHtml;
        }

        function MethodEnableOnClick(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).attr("data-rowid");

            $.post("UpdateMethodEnable", {
                Id: rowid,
                Enable: st
            }, function (data) {
                alert(data);
            });
        }

        function setRecordSheet(value, row, index) {
            var strHtml = "<button class='table-cell-button btn-rfs' data-Method-Id=" + row.id + " onclick='TableCellButton(this)'><i class='bi-tools'></i></button>";
            return strHtml;
        }

        function TableCellButton(obj) {
            $("#modalTitle").text("方法附件管理");

            $("#Sample-Method-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("/Common/ManageFile", {
                Domain: "Method",
                DomainId: $(obj.outerHTML).attr("data-Method-Id"),
                ModalID: "Sample-Method-Modal"
            },  function (datas) {
                $("#Sample-Method-Modal-Embed").html(datas);
            });
        }

        $("#Sample-Method-delMethod").click(function () {
            if (Sample_Method_Id == "") {
                return false;
            }

            $("#Sample-Method-DelMethod").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#Sample-Method-delMethod-delete").click(function () {
            $.post("postDeleteMethod", {
                Id: Sample_Method_Id
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $('#Sample-Method-Table').bootstrapTable('removeByUniqueId', Sample_Method_Id);  // 删除指定的行
                    Sample_Method_Id = "";  // 删除行后，将这个值置空，否则直接编辑信息就会报错。
                } else {
                    alert(data);
                }
            });

            $("#Sample-Method-DelMethod").modal('hide');
        });

        $("#Sample-Method-addMethod").click(function () {
            $("#modalTitle").text("添加方法");

            $("#Sample-Method-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddMethod", {
                ModalID: "Sample-Method-Modal"
            }, function (datas) {
                $("#Sample-Method-Modal-Embed").html(datas);
            });
        });

        // $("#Sample-Method-testSheet").click(function () {
        //     $("#modalTitle").text("电子记录单");

        //     $("#Sample-Method-Modal").modal({
        //         backdrop: 'static',
        //         keyboard: false
        //     });

        //     $("#SAMPLE-Method-Modal-SampleId").val('AA00000000101');
        //     $("#Sample-Method-Modal-Embed").load("https://localhost:44311/RecordSheet/表单1.html");

        //     //$.post("HtmltoPdf", {  // 这个代码可以放到审核按钮里，这样当单击审核时，该表单将生成为PDF文件并保存。
        //     //    url: "https://localhost:44311/RecordSheet/表单1.html?SampleId=AA00000000101"
        //     //}, function (data) {
        //     //});
        // });

        $("#Sample-Method-editMethod").click(function () {
            if (Sample_Method_Id.length == 0) {
                alert("请先选择需要编辑的方法！");
                return;
            }

            $("#modalTitle").text("编辑方法");

            $("#Sample-Method-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddMethod", {
                Id: Sample_Method_Id,
                ModalID: "Sample-Method-Modal"
            }, function (datas) {
                $("#Sample-Method-Modal-Embed").html(datas);
            });
        });

        $('#Sample-Method-Modal').on('hidden.bs.modal', function (event) {
            if (event.target == this) {
                if (Sample_Method_Id.length > 0) {
                    $.post("GetMethod", {
                        Id: Sample_Method_Id
                    }, function (data) {
                        var row = $('#Sample-Method-Table').bootstrapTable('getRowByUniqueId', Sample_Method_Id);
                        row.code = data.code;
                        row.name = data.name;
                        row.description = data.description;
                        row.type = data.type;
                        row.effectiveDate = data.effectiveDate;
                        row.recordSheet = data.recordSheet;
                        row.expireDate = data.expireDate;
                        row.version = data.version;
                        $('#Sample-Method-Table').bootstrapTable('updateByUniqueId', row);
                    });
                }
            }
        });

    </script>
}
